package com.apes.scm.masterdata.party.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.area.model.Area;
import com.apes.scm.masterdata.area.model.Country;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Branch;
import com.apes.scm.masterdata.frame.model.Dept;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

/**
 * 功能：人员
 *
 * @author HuJianLin
 * @create 2018-0-24 11:23
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Person.CONST_TABLE_NAME)
public class Person extends BaseModel {
    public static final String CONST_TABLE_NAME = "mas_person";

    @Id
    @Column(name = "id")
    @Comment("人员编码")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "人员")
    private long id;   //人员编码

    @Name
    @Transient
    private String name;

    public String getName() {
        if (party == null) return null;
        return party.getName();
    }

    @OneToOne(optional = false, fetch = FetchType.LAZY)
    @JoinColumn(name="party_id", referencedColumnName="id")
    @Comment("当事人编码")
    private Party party; //当事人

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "sex", referencedColumnName = "id")
    @Comment("性别")
    private Enumeration sex; //性别

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "birthday")
    @Comment("生日")
    private Date birthday; //生日

    @Column(name = "postal_address", length = 64)
    @Comment("通讯地址")
    private String postalAddress;  //通讯地址

    @Column(name = "postal_code", length = 32)
    @Comment("邮政编码")
    private String postalCode; //邮政编码

    @Column(name = "id_card", length = 32)
    @Comment("身份证号")
    private String idCard;  //身份证号

    @Column(name = "mobile_number", length = 32)
    @Comment("手机号")
    private String mobileNumber; //手机号

    @Column(name = "mobile_number2", length = 32)
    @Comment("手机号")
    private String mobileNumber2; //手机号

    @Column(name = "bank", length = 200)
    @Comment("开户行")
    private String bank; //开户行

    @Column(name = "bank_account", length = 32)
    @Comment("银行帐号")
    private String bankAccount; //银行帐号

    //居住地址
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "living_country_id", referencedColumnName = "id")
    @Comment("居住地址.国家")
    private Country livingCountry; //居住地址.国家

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "living_province", referencedColumnName = "id")
    @Comment("居住地址.省份")
    private Area livingProvince; //居住地址.省份

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "living_city_id", referencedColumnName = "id")
    @Comment("居住地址.城市")
    private Area livingCity; //居住地址.城市

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "living_county_id", referencedColumnName = "id")
    @Comment("居住地址.区县")
    private Area livingCounty; //居住地址.区县

    @Column(name = "living_address", length = 500)
    @Comment("居住地址.地址")
    private String livingAddress; //居住地址.地址

    /*---------------------------------------------------------------------
     * 员工信息
     *---------------------------------------------------------------------*/
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("所属公司")
    private Party company; //所属公司

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "work_branch_id", referencedColumnName = "id")
    @Comment("工作部门")
    private Branch workBranch; //工作部门

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "employee_state_id", referencedColumnName = "id")
    @Comment("员工状态")
    private Enumeration employeeState; //员工状态

    /*---------------------------------------------------------------------
     * 会员信息
     *---------------------------------------------------------------------*/
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "first_dept_id", referencedColumnName = "id")
    @Comment("首次消费部门")
    private Dept firstDept; //首次消费部门

    @Column(name = "accept_call_back", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("接受电话回访")
    private boolean acceptCallBack = true; //接受电话回访

    @Column(name = "accept_messages", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("接受短信")
    private boolean acceptMessages = true;  //接受短信

    @Column(name = "accept_We_Chat_push", columnDefinition = "number(1,0) default 1")
    @Comment("接受微信推送")
    private boolean acceptWeChatPush = true;  //接受微信推送

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "vip_state_id", referencedColumnName = "id")
    @Comment("会员状态")
    private Enumeration vipState; //会员状态（freezing：冻结; opening：启用） -- InsuranceFreeze：保险冻结(未使用)

    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("有效")
    private boolean valid = true; //有效

    @Name
    @Column(name = "ding_ding_user_id", length = 500)
    @Comment("钉钉用户ID")
    private String dingDingUserId; //钉钉用户ID

}
